﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections.Specialized;


namespace Piao
{
    public partial class WebProxyTest : Form
    {
        StringCollection proxyList;
        private delegate void UpdateTime(string time);
        List<Thread> threadList = new List<Thread>();
        public readonly object locker = new object();
        public WebProxyTest()
        {
            InitializeComponent();
            
            proxyList = Piao.Configurations.Instance.ProxyList;
            if (proxyList != null)
            {
                foreach (string p in proxyList)
                {
                    lst_Proxy.Items.Add(new ListViewItem(p) { Name = p });
                }

            }

        }


        private void button1_Click(object sender, EventArgs e)
        {
            lock (locker)
            {
                foreach (var item in threadList)
                {
                    if (item.IsAlive)
                        return;
                }
            }

            threadList.Clear();
            url = txt_url.Text;

            foreach (string p in proxyList)
            {
                Thread t = new Thread(ThreadStart);
                t.Start(p);


                lock (locker)
                {
                    threadList.Add(t);
                }
            }

        }


        string url = string.Empty;
        const int MAX_TRIES = 5;

        private void ThreadStart(object p)
        {
            string itemText = p.ToString();
            UpdateTime prepDel = new UpdateTime(t =>
            {
                var item = lst_Proxy.Items[itemText];
                if (item.SubItems.Count > 1)
                    item.SubItems.RemoveAt(1);
            });

            this.Invoke(prepDel, "");


            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            WebUtil web = new WebUtil();
            web.Proxy = new System.Net.WebProxy(itemText, true);

            for (int t = 0; t < MAX_TRIES; t++)
            {
                if (Thread.CurrentThread.ThreadState == ThreadState.AbortRequested)
                    return;

                System.Diagnostics.Debug.WriteLine(t + ":" + url);
                web.Get(url);
            }

            watch.Stop();

            double avg = watch.Elapsed.TotalMilliseconds / MAX_TRIES;

            TimeSpan ts = TimeSpan.FromMilliseconds(avg);


            UpdateTime del = new UpdateTime(t =>
                {
                    var item = lst_Proxy.Items[itemText];
                    item.SubItems.Add(t);

                });

            this.Invoke(del,
                        string.Format("{0:D2}:{1:D2}:{2:D3} ({3} errors)",
                        ts.Minutes, ts.Seconds, ts.Milliseconds, web.Failure));





        }



        protected override void OnClosing(CancelEventArgs e)
        {
            lock (locker)
            {
                foreach (var item in threadList)
                {
                    if (item.IsAlive)
                        item.Abort();
                }
            }

            base.OnClosing(e);
        }

    }
}
